import os
import cv2

count = 0
for file in os.listdir('/home/lixuan/workspace/dataset/scenesDec/images/train'):
    img = cv2.imread('/home/lixuan/workspace/dataset/scenesDec/images/train/{}'.format(file))
    H,W,_ = img.shape
    with open('/home/lixuan/workspace/dataset/scenesDec/labels/train/{}'.format(file.replace('jpg','txt'))) as f:
        datas = f.readlines()
        for data in datas:
            data = data.strip().split(' ')
            label = int(data[0]) + 1
            x = float(data[1]) * W
            y = float(data[2]) * H
            w = float(data[3]) * W
            h = float(data[4]) * H
            x1 = int(x - w / 2)
            y1 = int(y - h / 2)
            x2 = int(x1 + w)
            y2 = int(y1 + h)
            patch = img[y1:y2,x1:x2]
            cv2.imwrite('/home/lixuan/workspace/gz/{}/patch_{}.jpg'.format(label,count),patch)
            count += 1